<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="子曰: “学而时习之,不亦说乎?”  1. YYYY 和 yyyy 不一样以 2019年12月31日 举例：yyyy-MM-dd ：2019-12-31YYYY-MM-dd ：2020-12-31 相信这个你已经知道了，很多大佬都写过详细的讲解文章，也可直接看官方说明SimpleDateFormat 。 简言之：Y 指的是 Week year，表示的是这个周所属的年份；y 表示的才是我们日常使用">
<meta property="og:type" content="article">
<meta property="og:title" content="【安卓】唠点儿你不一定知道的小知识">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E3%80%90%E5%AE%89%E5%8D%93%E3%80%91%E5%94%A0%E7%82%B9%E5%84%BF%E4%BD%A0%E4%B8%8D%E4%B8%80%E5%AE%9A%E7%9F%A5%E9%81%93%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="子曰: “学而时习之,不亦说乎?”  1. YYYY 和 yyyy 不一样以 2019年12月31日 举例：yyyy-MM-dd ：2019-12-31YYYY-MM-dd ：2020-12-31 相信这个你已经知道了，很多大佬都写过详细的讲解文章，也可直接看官方说明SimpleDateFormat 。 简言之：Y 指的是 Week year，表示的是这个周所属的年份；y 表示的才是我们日常使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200222222658343.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2021-08-25T14:42:51.142Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.797Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Android Studio">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200222222658343.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E3%80%90%E5%AE%89%E5%8D%93%E3%80%91%E5%94%A0%E7%82%B9%E5%84%BF%E4%BD%A0%E4%B8%8D%E4%B8%80%E5%AE%9A%E7%9F%A5%E9%81%93%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【安卓】唠点儿你不一定知道的小知识 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E3%80%90%E5%AE%89%E5%8D%93%E3%80%91%E5%94%A0%E7%82%B9%E5%84%BF%E4%BD%A0%E4%B8%8D%E4%B8%80%E5%AE%9A%E7%9F%A5%E9%81%93%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【安卓】唠点儿你不一定知道的小知识
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>子曰: “学而时习之,不亦说乎?”</p>
</blockquote>
<h2 id="1-YYYY-和-yyyy-不一样"><a href="#1-YYYY-和-yyyy-不一样" class="headerlink" title="1. YYYY 和 yyyy 不一样"></a>1. YYYY 和 yyyy 不一样</h2><p>以 2019年12月31日 举例：<br>yyyy-MM-dd ：2019-12-31<br>YYYY-MM-dd ：2020-12-31</p>
<p>相信这个你已经知道了，很多大佬都写过详细的讲解文章，也可直接看官方说明<a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a> 。</p>
<p>简言之：Y 指的是 Week year，表示的是这个周所属的年份；y 表示的才是我们日常使用的年份。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html</a></p>
</blockquote>
<h2 id="2-getReadableDatabase-不是以只读方式打开数据库"><a href="#2-getReadableDatabase-不是以只读方式打开数据库" class="headerlink" title="2. getReadableDatabase 不是以只读方式打开数据库"></a>2. getReadableDatabase 不是以只读方式打开数据库</h2><p>Android 中 <code>getWritableDatabase()</code> 和 <code>getReadableDatabase()</code> 方法都可以获取到 SQLiteDatabase 实例。<br>但<code>getReadableDatabase()</code>并不是以只读方式打开数据库，而是先执行<code>getWritableDatabase()</code>，失败的情况下才以只读方式打开数据库.。<br>源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> SQLiteDatabase <span class="title">getReadableDatabase</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 执行 getWritableDatabase() , 若出现异常,以只读方式打开数据库</span></span><br><span class="line">        <span class="keyword">return</span> getWritableDatabase();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SQLiteException e) &#123;</span><br><span class="line">        <span class="keyword">if</span> (mName == <span class="keyword">null</span>) <span class="keyword">throw</span> e;  </span><br><span class="line">    &#125;</span><br><span class="line">    SQLiteDatabase db = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// ... </span></span><br><span class="line">        <span class="comment">// 以只读方式打开数据库</span></span><br><span class="line">        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);</span><br><span class="line">        <span class="comment">// ... </span></span><br><span class="line">        mDatabase = db;</span><br><span class="line">        <span class="keyword">return</span> mDatabase;</span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        <span class="comment">// ... </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-子线程未必不能更新UI"><a href="#3-子线程未必不能更新UI" class="headerlink" title="3. 子线程未必不能更新UI"></a>3. 子线程未必不能更新UI</h2><p>Android的UI访问是没有加锁的，多线程访问时并不安全。所以规定只能在UI线程中访问UI。<br>负责检查线程的就是 <code>ViewRootImpl</code> 的 <code>checkThread()</code> 方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">checkThread</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (mThread != Thread.currentThread()) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> CalledFromWrongThreadException(</span><br><span class="line">                <span class="string">&quot;Only the original thread that created a view hierarchy can touch its views.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然而 <code>ViewRootImpl</code> 的创建在 <code>onResume()</code> 回调之后。那么在 <code>onResume()</code> 之前，子线程里也是可以更新 UI 的 。<br>即使是 ViewRootImpl 创建后，只要不调用 <code>checkThread()</code>，子线程里更新也并不会报错。</p>
<blockquote>
<p>然而，大家开发的时候还是不要在子线程更新UI。</p>
</blockquote>
<h2 id="4-代码-new-的-View-没有id"><a href="#4-代码-new-的-View-没有id" class="headerlink" title="4. 代码 new 的 View 没有id"></a>4. 代码 new 的 View 没有id</h2><p>Android布局文件中通过 @+id 的方式，可以在 R文件 中生成对应的一个Int值，用于在运行时保证资源唯一性，但动态在代码中 new 的 View 没有 id 。<br>如果你有需求使用它的id，可以调用 View 的 <code>generateViewId()</code> 方法来生成 id(API17+) ，而非用随机数产生或手写一个具体的值。</p>
<h2 id="5-View-的-getContext-返回的未必是Activity"><a href="#5-View-的-getContext-返回的未必是Activity" class="headerlink" title="5. View 的 getContext 返回的未必是Activity"></a>5. View 的 getContext 返回的未必是Activity</h2><p>Activity中setContentView时一定是Activity;<br>通过 new View、View.inflate、LayoutInflater.inflate 这几种方式添加View，我们传参时传的是什么context, View中的就是什么Context.</p>
<p>在5.0系统版本以下的手机，且 Activity 是继承自 AppCompatActivity 的，那么View的getConext方法，返回的就不是Activity而是TintContextWrapper。</p>
<blockquote>
<p>参考博文：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5Xj6O9wV_EnynpdnB5mJ5A">https://mp.weixin.qq.com/s/5Xj6O9wV_EnynpdnB5mJ5A</a>  (南尘)</p>
</blockquote>
<h2 id="6-RemoteViews-和-View-没啥关系"><a href="#6-RemoteViews-和-View-没啥关系" class="headerlink" title="6. RemoteViews 和 View 没啥关系"></a>6. RemoteViews 和 View 没啥关系</h2><p>RemoteViews 提供了一组基础的操作用于跨进程更新，主要用于通知栏和桌面小部件的开发。从名称来看，感觉应该是一种远程的View。其实不然，源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RemoteViews</span> <span class="keyword">implements</span> <span class="title">Parcelable</span>, <span class="title">Filter</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>总而言之，RemoteViews 就是为跨进程操作控件而提供一系列方法的一个类。</p>
<h2 id="7-boolean-类型占几个字节"><a href="#7-boolean-类型占几个字节" class="headerlink" title="7. boolean 类型占几个字节"></a>7. boolean 类型占几个字节</h2><p>Java中 boolean 表示的实际信息是一位：1表示true，0表示false。但是，Java规范 <a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">数据类型文档</a> 没有精确定义内存中布尔变量的实际大小。</p>
<p>其大小与虚拟机相关，可以肯定的是，不会是 1 个 bit 。 Java 虚拟机的建议如下：<br>1.boolean 类型被编译成 int 类型来使用，占 4 个 byte 。<br>2.boolean 数组被编译成 byte 数组类型，每个 boolean 数组成员占 1 个 byte </p>
<blockquote>
<p>参考博文：<a target="_blank" rel="noopener" href="https://binkery.com/archives/346.html">https://binkery.com/archives/346.html</a></p>
</blockquote>
<h2 id="8-RecyclerView-布局文件可指定layoutManager跟spanCount"><a href="#8-RecyclerView-布局文件可指定layoutManager跟spanCount" class="headerlink" title="8. RecyclerView 布局文件可指定layoutManager跟spanCount"></a>8. RecyclerView 布局文件可指定layoutManager跟spanCount</h2><p>RecyclerView 布局文件可指定layoutManager跟spanCount</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">declare-styleable</span> <span class="attr">name</span>=<span class="string">&quot;RecyclerView&quot;</span>&gt;</span>        </span><br><span class="line">    <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">&quot;layoutManager&quot;</span> <span class="attr">format</span>=<span class="string">&quot;string&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">&quot;android:orientation&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">&quot;spanCount&quot;</span> <span class="attr">format</span>=<span class="string">&quot;integer&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">&quot;reverseLayout&quot;</span> <span class="attr">format</span>=<span class="string">&quot;boolean&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">attr</span> <span class="attr">name</span>=<span class="string">&quot;stackFromEnd&quot;</span> <span class="attr">format</span>=<span class="string">&quot;boolean&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">declare-styleable</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>它的属性attr里面可以指定layoutManager，spanCount,orientation的。不必我们在代码里设置。</p>
<h2 id="9-9-patch-图片是有-padding-的"><a href="#9-9-patch-图片是有-padding-的" class="headerlink" title="9.  9-patch 图片是有 padding 的"></a>9.  9-patch 图片是有 padding 的</h2><p>NinePatchDrawable 图形是一种可拉伸的位图，可用作视图的背景。Android 会自动调整图形的大小以适应视图的内容。包含一个额外的 1 像素边框，必须使用 9.png 扩展名将其保存在项目的 res/drawable/ 目录下。<br><img src="https://img-blog.csdnimg.cn/20200222222658343.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poZW5nX3dlaWNoYW8=,size_16,color_FFFFFF,t_70" alt="9patch图片"><br>线的作用：<br>左，上：定义允许复制图片的哪些像素来拉伸图片。<br>右，下：定义图片中允许放置视图内容的相对区域。</p>
<p>因此，9-patch 图片可能带有 padding，<strong>如果控件没有明确设置，图片的 padding 会作为控件的 padding</strong> 。</p>
<p>所以，有时候，<code>android:padding=&quot;0dp&quot;</code> 该写也是得写的。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://developer.android.com/guide/topics/graphics/drawables#nine-patch">https://developer.android.com/guide/topics/graphics/drawables#nine-patch</a></p>
</blockquote>
<h2 id="10-硬件加速不是哪里都能开关"><a href="#10-硬件加速不是哪里都能开关" class="headerlink" title="10. 硬件加速不是哪里都能开关"></a>10. 硬件加速不是哪里都能开关</h2><p>硬件加速，直观上说就是依赖GPU实现图形绘制加速。由于 GPU 的引入不仅提高了绘制效率，还由于绘制机制的改变，极大地提高了界面内容改变时的刷新效率。<br>在 Android4.0 开始默认开启硬件加速，也可以手动控制打开关闭：</p>
<p>需要注意的是：<br><strong>硬件加速在Window级只能开不能关；View级只能关不能开。</strong></p>
<p><strong>Application 和 Activity 控制</strong><br>在 AndroidManifest 文件中 Application 或 Activity 节点添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">android:hardwareAccelerated=<span class="string">&quot;true&quot;</span></span><br></pre></td></tr></table></figure>
<p><strong>Window控制</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,</span><br><span class="line">WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)</span><br></pre></td></tr></table></figure>
<p><strong>View控制</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">view.setLayerType(View.LAYER_TYPE_SOFTWARE,<span class="keyword">null</span>);</span><br></pre></td></tr></table></figure>

<p><strong>查询是否开启硬件加速</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">View.isHardwareAccelerated()</span><br><span class="line">Canvas.isHardwareAccelerated()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>参考博文：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/9cd7097a4fcf">https://www.jianshu.com/p/9cd7097a4fcf</a></p>
</blockquote>
<h2 id="11-用-getVisibility-判断用户是否能看见并不好"><a href="#11-用-getVisibility-判断用户是否能看见并不好" class="headerlink" title="11. 用 getVisibility() 判断用户是否能看见并不好"></a>11. 用 getVisibility() 判断用户是否能看见并不好</h2><p>getVisibility()只判断它自身是否是显示状态。但是如果它的父级不可见呢？<br>用 <code>isShown()</code> 方法更合适些，会先判断当前 View 的flag， 然后循环拿到父View，判断是不是可见。只要有一个是不可见的，就返回false。<br>源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isShown</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    View current = <span class="keyword">this</span>;</span><br><span class="line">    <span class="comment">//noinspection ConstantConditions</span></span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> ((current.mViewFlags &amp; VISIBILITY_MASK) != VISIBLE) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ViewParent parent = current.mParent;</span><br><span class="line">        <span class="keyword">if</span> (parent == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>; <span class="comment">// We are not attached to the view root</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!(parent <span class="keyword">instanceof</span> View)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        current = (View) parent;</span><br><span class="line">    &#125; <span class="keyword">while</span> (current != <span class="keyword">null</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<p>很多知识点都来自于网上大佬们的博客，有些已经忘记从哪里看到的了，在此向大佬们致敬！</p>
<p>如果本文对你有所帮助，还望可以点个赞哈~~</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/Android-Studio/" rel="tag"># Android Studio</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E3%80%90Vue%E3%80%91%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2/" rel="prev" title="【Vue】项目部署">
      <i class="fa fa-chevron-left"></i> 【Vue】项目部署
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E3%80%90%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90%E3%80%91%E4%B8%80%E8%B5%B7%E6%9D%A5%E7%9C%8B%E4%B8%AA%E9%80%BC%E6%A0%BC%E5%84%BF%E5%BE%88%E9%AB%98%E7%9A%84%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86%E9%A1%B9%E7%9B%AE/" rel="next" title="【项目推荐】一起来看个逼格儿很高的个人简历项目">
      【项目推荐】一起来看个逼格儿很高的个人简历项目 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-YYYY-%E5%92%8C-yyyy-%E4%B8%8D%E4%B8%80%E6%A0%B7"><span class="nav-number">1.</span> <span class="nav-text">1. YYYY 和 yyyy 不一样</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-getReadableDatabase-%E4%B8%8D%E6%98%AF%E4%BB%A5%E5%8F%AA%E8%AF%BB%E6%96%B9%E5%BC%8F%E6%89%93%E5%BC%80%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">2.</span> <span class="nav-text">2. getReadableDatabase 不是以只读方式打开数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AD%90%E7%BA%BF%E7%A8%8B%E6%9C%AA%E5%BF%85%E4%B8%8D%E8%83%BD%E6%9B%B4%E6%96%B0UI"><span class="nav-number">3.</span> <span class="nav-text">3. 子线程未必不能更新UI</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BB%A3%E7%A0%81-new-%E7%9A%84-View-%E6%B2%A1%E6%9C%89id"><span class="nav-number">4.</span> <span class="nav-text">4. 代码 new 的 View 没有id</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-View-%E7%9A%84-getContext-%E8%BF%94%E5%9B%9E%E7%9A%84%E6%9C%AA%E5%BF%85%E6%98%AFActivity"><span class="nav-number">5.</span> <span class="nav-text">5. View 的 getContext 返回的未必是Activity</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-RemoteViews-%E5%92%8C-View-%E6%B2%A1%E5%95%A5%E5%85%B3%E7%B3%BB"><span class="nav-number">6.</span> <span class="nav-text">6. RemoteViews 和 View 没啥关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-boolean-%E7%B1%BB%E5%9E%8B%E5%8D%A0%E5%87%A0%E4%B8%AA%E5%AD%97%E8%8A%82"><span class="nav-number">7.</span> <span class="nav-text">7. boolean 类型占几个字节</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-RecyclerView-%E5%B8%83%E5%B1%80%E6%96%87%E4%BB%B6%E5%8F%AF%E6%8C%87%E5%AE%9AlayoutManager%E8%B7%9FspanCount"><span class="nav-number">8.</span> <span class="nav-text">8. RecyclerView 布局文件可指定layoutManager跟spanCount</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-9-patch-%E5%9B%BE%E7%89%87%E6%98%AF%E6%9C%89-padding-%E7%9A%84"><span class="nav-number">9.</span> <span class="nav-text">9.  9-patch 图片是有 padding 的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-%E7%A1%AC%E4%BB%B6%E5%8A%A0%E9%80%9F%E4%B8%8D%E6%98%AF%E5%93%AA%E9%87%8C%E9%83%BD%E8%83%BD%E5%BC%80%E5%85%B3"><span class="nav-number">10.</span> <span class="nav-text">10. 硬件加速不是哪里都能开关</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-%E7%94%A8-getVisibility-%E5%88%A4%E6%96%AD%E7%94%A8%E6%88%B7%E6%98%AF%E5%90%A6%E8%83%BD%E7%9C%8B%E8%A7%81%E5%B9%B6%E4%B8%8D%E5%A5%BD"><span class="nav-number">11.</span> <span class="nav-text">11. 用 getVisibility() 判断用户是否能看见并不好</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
